<?php
class UsersController extends AppController
{
    var $name='Users';
    var $components = array('Auth');
     var $layout = 'admin';
    var $helpers = array('Javascript','Form','Paginator');
    
    function beforeFilter()
    {
        $this->Auth->loginError = '密码或帐号错误,或者帐号无管理员权限';
        $this->Auth->authError='还未登录或者你没有管理员权限';
        $this->Auth->userScope = array('admin_leve'=>1);
        $this->Auth->authorize='controller';
    }
    
    function isAuthorized(){
        if($this->Auth->user('admin_leve')>0)
            return true;
        else
            return false;
    }
    
    function admin_index($page=1)
    {
        if(!is_numeric($page)){
            $this->flash('页面参数错误','index',5);
            return;
        }
         $this->paginate=array(
            'page' => $page,
            'limit' =>25,
            'order' => array('User.modified' =>  'desc')
            );
          $ul=$this->paginate();
          $this->set('ul',$ul);
    }
    
    function admin_del($id)
    {
        if(empty($id) or !is_numeric($id)){
            $this->flash('请指定要删除的用户',$oldurl);
            return;
        }
        if($this->User->del($id)){
            $this->flash('删除成功','index',5);
            return;
         }else{
            $this->flash('未能删除成功','index',5);
            return;
          }
    }
    
      function admin_delm($ids)
    {
        if(empty($ids) or !is_string($ids)){
            $this->flash('请选择要用户','index',5);
            return;
        }
        $idarr=explode(',',$ids);
        if($this->User->deleteAll(array('id' => $idarr))){
            $this->flash('成功删除多个用户',"index",5);
            return;
        }
    }
    
     function admin_edit($id=null)
    {
        if(!empty($this->data)){
            if(!empty($this->data['User']['password2'])){
                $this->data['User']['password']=$this->Auth->password($this->data['User']['password2']);
                 $this->data['User']['password2Hash']=$this->data['User']['password'];
            }else{
                unset($this->data['User']['password']);
                unset($this->User->validate['password']);
                unset($this->User->validate['password2']);
            }       
            if($this->User ->save($this->data)){
                $this -> flash("用户信息修改成功。",'index',5);
                return;
            }
        }else{
            if(empty($id)){
                $this -> flash('请选择要修改的用户','index',5);
                return;
             }
             $this->data = $this -> User -> read(null,$id);
             if(empty($this -> data)){
                $this -> flash('所选择的用户并不存在','index',5);
                return;
            }
        }
        if(!empty($this->data['User']['admin_leve']))
              $this->set('checked','checked');
         else
               $this->set('checked','');
        $this->pageTitle ='修改用户【'.$this->data['User']['username'].'】的信息';
    }
    
    function admin_login()
    {
        $this->layout=null;
    }
    
}
?>